<template>
  <div class="api-node">
    <!-- 删除按钮 -->
    <el-button 
      class="delete-btn" 
      size="small" 
      type="danger" 
      circle
      @click.stop="handleDelete"
    >
      <el-icon><Close /></el-icon>
    </el-button>
    
    <div class="node-header">
      <el-icon class="node-icon"><Link /></el-icon>
      <span class="node-title">{{ data.label }}</span>
    </div>
    <div class="node-content">
      <div class="api-method" v-if="interfaceInfo">
        <el-tag :type="getMethodType(interfaceInfo.method)" size="small">
          {{ interfaceInfo.method }}
        </el-tag>
      </div>
      <div class="api-url" v-if="interfaceInfo">
        {{ truncateUrl(interfaceInfo.url) }}
      </div>
      <div class="node-description" v-else>
        点击配置接口
      </div>
    </div>
    
    <!-- 连接点 -->
    <Handle
      type="target"
      position="top"
      class="custom-handle target-handle"
    />
    <Handle
      type="source"
      position="bottom"
      class="custom-handle source-handle"
    />
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { Handle } from '@vue-flow/core'
import { Link, Close } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'

const props = defineProps({
  data: {
    type: Object,
    required: true
  },
  id: {
    type: String,
    required: true
  },
  interfaces: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['delete'])

// 根据接口ID获取接口信息
const interfaceInfo = computed(() => {
  if (!props.data.config?.interfaceId) return null
  return props.interfaces.find(iface => iface.id === props.data.config.interfaceId)
})

// 获取HTTP方法对应的标签类型
const getMethodType = (method) => {
  const typeMap = {
    'GET': 'success',
    'POST': 'primary',
    'PUT': 'warning',
    'DELETE': 'danger',
    'PATCH': 'info'
  }
  return typeMap[method] || 'info'
}

// 截断URL显示
const truncateUrl = (url) => {
  if (!url) return ''
  return url.length > 30 ? url.substring(0, 30) + '...' : url
}

// 处理删除节点
const handleDelete = () => {
  ElMessageBox.confirm(
    '确定要删除这个API节点吗？此操作不可撤销。',
    '删除节点',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    emit('delete', { id: props.id, ...props })
    ElMessage.success('节点已删除')
  }).catch(() => {
    // 用户取消删除
  })
}
</script>

<style scoped>
.api-node {
  background: linear-gradient(135deg, #409eff, #66b1ff);
  color: white;
  border-radius: 12px;
  padding: 12px 16px;
  min-width: 160px;
  max-width: 200px;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
  position: relative;
}

.node-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.node-icon {
  font-size: 16px;
}

.node-title {
  font-weight: 600;
  font-size: 14px;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.node-content {
  font-size: 12px;
}

.api-method {
  margin-bottom: 4px;
}

.api-url {
  color: rgba(255, 255, 255, 0.9);
  font-family: monospace;
  font-size: 11px;
  word-break: break-all;
}

.node-description {
  color: rgba(255, 255, 255, 0.8);
  font-style: italic;
}

.custom-handle {
  width: 12px;
  height: 12px;
  border: 2px solid white;
  background: #409eff;
}

.target-handle {
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
}

.source-handle {
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
}

.delete-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px !important;
  height: 20px !important;
  min-height: 20px !important;
  padding: 0 !important;
  z-index: 10;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.api-node:hover .delete-btn {
  opacity: 1;
}

.delete-btn .el-icon {
  font-size: 12px;
}
</style>